<?php

namespace app\web\services;

use think\App;
use think\facade\Request;
use think\Service;
use app\web\model\NavModel;
use app\web\model\LotterytypeModel;
use app\web\model\NticeqkModel;
use app\web\model\LerrtyerkModel;
use app\admin\model\AgentModel;
use app\admin\model\TestModel;
use app\admin\model\StandmoneyModel;
class IndexService extends Service
{
    public function __construct(Request $request = null, App $app = null)
    {
        $this->NavModel = new NavModel();
        $this->LotterytypeModel = new LotterytypeModel;
        $this->NticeqkModel = new NticeqkModel;
        $this->LerrtyerkModel = new LerrtyerkModel;
        $this->AgentModel = new AgentModel;
        $this->TestModel = new TestModel;
        $this->StandmoneyModel = new StandmoneyModel;
        parent::__construct($request, $app);
    }

    /**
     * 获取标题导航
     */
    public function getNavMsg()
    {
        $res = $this->NavModel->getNav();
        if (!$res) {
            return $this->_getResponse("NUKNOW_EORRE");
        }
        $pathinfo = '/' . Request::pathinfo();
        if($pathinfo == "//"){
            $pathinfo = '/web/index/index';
        }
        foreach ($res as $k => $v) {
            if ($v['url'] == $pathinfo) {
                $res[$k]['states'] = 1;
            } else {
                $res[$k]['states'] = 0;
            }
        }
        return $this->_getResponse("SUCCESS", $res);
    }


    /**
     * @param 彩总列表
     */
    public function lotterytype(){
        $data = $this->LotterytypeModel->lottery();
        return $data;
    }


    /***
     * @param 获取公告
     */
    public function Nicton(){
       $where = [['enable','eq',1],['types','eq',1],['tlaisrol','eq',2]];
        $data = $this->NticeqkModel->datalist($where);
        if(!$data){
            $this->_getResponse("NUKNOW_EORRE");
        }
        $array = array();
        foreach ($data as $value){
            $value['Bulletin'] = strip_tags(htmlspecialchars_decode($value['Bulletin'],ENT_QUOTES));
            $array[] = $value;
        }
        return $array;
    }


    /**
     * @param 获取网站信息
     */
    public function weblist(){
        $data = $this->LerrtyerkModel->weblist();
        $data['aboutus'] = strip_tags(htmlspecialchars_decode($data['aboutus'],ENT_QUOTES));
        return $data;
    }


    /**
     * @param 获取代理信息
     */
    public function agent(){
        $where = [['enable','eq',1]];
        $data = $this->AgentModel->webagent($where);
        return $data;
    }


    /**
     * @param 获取代理信息内容
     */
    public function sectrt(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        $id = $post['id'];
        if(empty($id)){
            return $this->_getResponse("PARAM_EORRE");
        }
        $where = [['id','eq',$id]];
        $data = $this->AgentModel->dlsist($where);
        if(!$data){
            return $this->_getResponse("DATA_ERROR");
        }
        $data['content'] = htmlspecialchars_decode($data['content']);
        return $this->_getResponse("SUCCESS",$data);
    }


    /**
     * @param 获取案例头条
     */
    public function Newtitle(){
        $where = [['enable','eq',1],['types','eq',1]];
        $wheres = [['enable','eq',1],['types','eq',2]];
        $newcase = $this->TestModel->Newtitle($where); // 获取案例
        $brief = $this->TestModel->NewBrief($wheres);//获取简介
        $arr['newcase'] = $newcase;
        $arr['brief'] = $brief;
       return $arr;
    }


    /**
     * @param 获取案例列表
     */
    public function beslist(){
        $where = [['enable','eq',1],['types','eq',1]];
        $data = $this->TestModel->beslist($where);
        return $data;
    }


    /**
     * @param 获取简介列表
     */
    public function brolist(){
        $where = [['enable','eq',1],['types','eq',2]];
        $data = $this->TestModel->beslist($where);
        return $data;
    }


    /**
     * @param 获取收费列表
     */
    public function listmoney(){
        $where = [['enable','eq',1]];
        $data = $this->StandmoneyModel->listmoney($where);
        return $data;
    }


    /**
     * @param 获取当前案例信息
     */
    public function movert(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        $id = $post['id'];
        if(empty($id)){
            return $this->_getResponse("PARAM_EORRE");
        }
        $where = [['id','eq',$id]];
        $data = $this->TestModel->dlsist($where);
        if(!$data){
            return $this->_getResponse("DATA_ERROR");
        }
        $data['content'] = htmlspecialchars_decode($data['content']);
        return $this->_getResponse("SUCCESS",$data);
    }


    /**
     * @param $key
     * @param array $data
     * @return mixed
     */
    protected function _getResponse($key, $data = [])
    {
        $maps = [
            "SUCCESS" => ["status" => 0, "msg" => "sccuess", "data" => $data],
            "PARAM_EORRE" => ["status" => 1, "msg" => "参数错误", "data" => $data],
            "NUKNOW_EORRE" => ["status" => 2, "msg" => "未知错误", "data" => $data],
            "NO_POST" => ["status" => 4, "msg" => "非法请求", "data" => $data],
            "DATA_ERROR" => ["status" => 7, "msg" => "数据不存在", "data" => $data]
        ];
        return $maps[$key];
    }

}